package com.icoopoo.modules.student.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icoopoo.modules.student.model.Student;
import com.icoopoo.modules.student.service.StudentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("student")
public class StudentController{

	
	Log loger = LogFactory.getLog(StudentController.class);
	
	
	private static int testStaticArgc = 1;
	
	@Autowired
	private StudentService service;
	
	@ResponseBody
	@RequestMapping(value = "/s",method = RequestMethod.GET)
	public Map<String, Object> findBmsHistoryMainLine(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String, Object>();
		//e.printStackTrace();
		result.put("RESULT_CODE", "1");
		result.put("RESULT_MSG", "系统异常！");
		System.out.println("************************");
		System.out.println("************************");
		System.out.println("************************");
		System.out.println("************************");
		System.out.println("************************");
		System.out.println("************************");
		System.out.println("************************");
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/get" ,method = RequestMethod.GET)
	public Map<String, Object> getStudentById(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String, Object>();
		
		String id = request.getParameter("id");
		result.put("msg", "0");
		result.put("data", service.getStudentById(id));
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.GET)
	public Map<String, Object> saveStudent(HttpServletRequest request, HttpServletResponse response,Student stu) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", "0");
		try{
			service.saveStudent(stu);
			System.out.println("保存成功");
		}catch(Exception e){
			result.put("code", "1");
			return result;
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update",method = RequestMethod.GET)
	public Map<String, Object> updateStudent(HttpServletRequest request, HttpServletResponse response,Student stu) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", "0");

		try{
			service.updateStudent(stu);
			System.out.println("更新成功");
		}catch(Exception e){
			result.put("code", "1");
			return result;
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/del",method = RequestMethod.GET)
	public Map<String, Object> deleteStudentById(HttpServletRequest request, HttpServletResponse response,String id) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
			System.out.println("********del  start*********");
			service.deleteStudentById(id);
			System.out.println("删除成功");
			loger.info("logger***********删除成功");
		}catch(Exception e){
			e.printStackTrace();
			result.put("code", "1");
			loger.info("删除异常", e);
			return result;
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/static",method = RequestMethod.GET)
	public Map<String, Object> findStaticArgs(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String, Object>();
		//e.printStackTrace();
		result.put("RESULT_CODE", "1");
		result.put("RESULT_MSG", "系统异常！");
		System.out.println("**********测试静态变量的值，spring的单例模式 测试  **************");
		testStaticArgc++;
		System.out.println("testStaticArgc =" + testStaticArgc);
		System.out.println("************************");
		return result;
	}
}
